package com.xu.book.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.java.accessibility.util.EventID;
import com.xu.book.bean.BorrowCard;
import com.xu.book.bean.Student;
import com.xu.book.service.IBorrowCardService;
import com.xu.book.service.IStudentService;
import com.xu.book.service.impl.BorrowCardServiceimpl;
import com.xu.book.service.impl.IStudentServiceimpl;
import com.xu.sys.bean.SysUser;
import com.xu.sys.servlet.BaseServlet;
import com.xu.sys.utils.Constant;
import com.xu.sys.utils.DateUtils;
import com.xu.sys.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "borrowCardServlet",urlPatterns = {"/book/borrowCardServlet"})
public class BorrowCardServlet extends BaseServlet {
    private IBorrowCardService service =new BorrowCardServiceimpl();
    private IStudentService studentService = new IStudentServiceimpl();
    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        super.list(req, resp);//处理分页操作
        //获取当前的用户信息
        service.listPage(pageUtils,getCurrentLoginUser(req,resp));
        req.setAttribute(Constant.LIST_PAGE_UTILS,pageUtils);
        req.getRequestDispatcher("/book/borrowCard/list.jsp").forward(req,resp);
    }

    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取表单提交的数据
        String id = req.getParameter(Constant.PARAMETER_ID);
        String stuid = req.getParameter("stuid");
        String starttime = req.getParameter("starttime");
        String endtime = req.getParameter("endtime");
        String state = req.getParameter("state");
//        System.out.println(starttime);
//        System.out.println(endtime);
        BorrowCard card = new BorrowCard();
        if (StringUtils.isNotEmpty(state)){
            card.setState(Integer.parseInt(state));
        }else {
            card.setState(0);//如果状态为空设置默认值为0
        }
        if (StringUtils.isNotEmpty(stuid)) {
            Integer stuId = Integer.parseInt(stuid);
            Student stu =studentService.findById(stuId);
            card.setId(stuId);
            card.setStuname(stu.getName());
            card.setUserID(stu.getAccount());
            card.setStuid(stu.getId());
//            System.out.println(stu.getAccount());
        }
        if (StringUtils.isNotEmpty(starttime)){
            //做日期格式的转换
//            System.out.println(DateUtils.stringToDate(starttime,DateUtils.DATE_PARTTERN1));
            card.setStarttime(DateUtils.stringToDate(starttime,DateUtils.DATE_PARTTERN1));
        }
        if (StringUtils.isNotEmpty(endtime)){
//            System.out.println(DateUtils.stringToDate(endtime,DateUtils.DATE_PARTTERN1));
            card.setEndtime(DateUtils.stringToDate(endtime,DateUtils.DATE_PARTTERN1));
        }
        if (StringUtils.isNotEmpty(id)){
            //更新
            card.setId(Integer.parseInt(id));
            service.updateById(card);
        }else {
            service.save(card);
        }



        resp.sendRedirect("/book/borrowCardServlet?action=list");
    }

    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //需要查询出所有的学生信息
        List<Student> list = studentService.list();
        req.setAttribute("stus",list);
        String id = req.getParameter(Constant.PARAMETER_ID);
        if (StringUtils.isNotEmpty(id)){
            //更新
            BorrowCard entity = service.findById(Integer.parseInt(id));
            req.setAttribute(Constant.UPDATE_ENTITY,entity);
        }
        //进入更新或者添加页面
        req.getRequestDispatcher("/book/borrowCard/addOrUpdate.jsp").forward(req,resp);
    }

    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter(Constant.PARAMETER_ID);
        BorrowCard entity = service.findById(Integer.parseInt(id));
        entity.setState(3);
        service.updateById(entity);
        PrintWriter writer = resp.getWriter();
        writer.write("ok");
        writer.flush();

    }

    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }

    //检查当前用户是否有可用的借书卡
    public void checkHaveCard(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        //根据当前登录的用户 查询对应的借书卡
        List<BorrowCard> list = service.listCanuseCard(getCurrentLoginUser(req,resp).getId());
        Map<String,Object> map = new HashMap<>();
        String msg = "error";
        if (list != null && list.size() > 0){
             msg = "ok";
        }
        map.put("msg",msg);
        map.put("cards",list);
        //把这个map转换为json数据
        String s = JSONObject.toJSONString(map);
        //设置响应的编码
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(s);
        writer.flush();

    }
}
